-
-
Notifications
You must be signed in to change notification settings - Fork 18
Add orderBy, first and last parameters to groupedAggregates #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v5
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really thorough, I like it! I wonder how we avoid type explosion with this - perhaps we should have an opt-in to the feature? I don't think index behaviours can save us here. This plugin already adds a lot of types to the schema 😅
For now let's drop the last argument.
| - `first` / `last` – slice the ordered groups, returning only the leading or | ||
| trailing `n` groups. | ||
|
|
||
| Always pair `first` or `last` with an explicit `orderBy` so PostgreSQL can | ||
| deterministically rank the groups before trimming them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest that we remove last - if they need that they should do the inverse order.
| When using `last`, be sure to supply an `orderBy` so the database can produce a | ||
| deterministic ordering before the tail slice is applied. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should default to ordering by the group by clauses; I didn't realise we didn't do that already.
| orderBy: [SUM_POINTS_ASC] | ||
| last: 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| orderBy: [SUM_POINTS_ASC] | |
| last: 2 | |
| orderBy: [SUM_POINTS_ASC] | |
| first: 2 |
| When using `last`, be sure to supply an `orderBy` so the database can produce a | ||
| deterministic ordering before the tail slice is applied. | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| When using `last`, be sure to supply an `orderBy` so the database can produce a | |
| deterministic ordering before the tail slice is applied. |
| exports[`GroupedAggregatesOrderBy: sql 1`] = ` | ||
| [ | ||
| "select | ||
| (coalesce(sum(__match_stats__."points"), '0'))::text as "0", | ||
| __match_stats__."player_id"::text as "1" | ||
| from "test"."match_stats" as __match_stats__ | ||
| group by __match_stats__."player_id" | ||
| order by coalesce(sum(__match_stats__."points"), '0') desc | ||
| limit 2;", | ||
| ] | ||
| `; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 Where's the other query...?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is that when switching to last with reverse ordering, it became the same exact query for the database and it was deduplicated so it was never executed.
| last: { | ||
| type: build.graphql.GraphQLInt, | ||
| description: build.wrapDescription( | ||
| "Only include the last `n` grouped aggregates.", | ||
| "arg" | ||
| ), | ||
| applyPlan: EXPORTABLE( | ||
| () => | ||
| function ( | ||
| _$parent, | ||
| $pgSelect: PgSelectStep<any>, | ||
| arg | ||
| ) { | ||
| const selectAny = $pgSelect as any; | ||
| const originalAssert = | ||
| selectAny.assertCursorPaginationAllowed; | ||
| try { | ||
| selectAny.assertCursorPaginationAllowed = () => {}; | ||
| $pgSelect.setLast(arg.getRaw()); | ||
| } finally { | ||
| selectAny.assertCursorPaginationAllowed = | ||
| originalAssert; | ||
| } | ||
| }, | ||
| [] | ||
| ), | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| last: { | |
| type: build.graphql.GraphQLInt, | |
| description: build.wrapDescription( | |
| "Only include the last `n` grouped aggregates.", | |
| "arg" | |
| ), | |
| applyPlan: EXPORTABLE( | |
| () => | |
| function ( | |
| _$parent, | |
| $pgSelect: PgSelectStep<any>, | |
| arg | |
| ) { | |
| const selectAny = $pgSelect as any; | |
| const originalAssert = | |
| selectAny.assertCursorPaginationAllowed; | |
| try { | |
| selectAny.assertCursorPaginationAllowed = () => {}; | |
| $pgSelect.setLast(arg.getRaw()); | |
| } finally { | |
| selectAny.assertCursorPaginationAllowed = | |
| originalAssert; | |
| } | |
| }, | |
| [] | |
| ), | |
| }, |
More context on #75. Closes #75
Basically adds
orderBy,firstandlastparameters togroupedAggregates.orderByaccepts any possible field aggregations. Note that I still don't have a very good understanding of the project, this PR is party AI generated and partly from copy pasting various concepts around, would appreciate deep review from someone who understands the code better to see if there can better ways.Performance impact
Slight performance impact in startup time due to added(18 entries per numerical fields in AggregatesOrderBy enum)
Security impact
Shouldn't be.
Checklist
yarn lint:fixpasses. - I'm working on estlint to run properlyyarn testpasses.RELEASE_NOTES.mdfile (if one exists). - File doesn't exist